package com.example.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.commonutils.JwtUtils;
import com.example.commonutils.MD5;
import com.example.servicebase.exceptionhandler.CustomException;
import com.example.ucenter.entity.Member;
import com.example.ucenter.entity.vo.RegisterVo;
import com.example.ucenter.mapper.MemberMapper;
import com.example.ucenter.service.MemberService;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-06-01
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public String login(Member member) {
        //拿出手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();
        //判断是否为空
        if (StringUtil.isNullOrEmpty(mobile)||StringUtil.isNullOrEmpty(password)){
            throw new CustomException(20001,"信息不全");
        }
        //不为空则继续验证手机号
        QueryWrapper<Member> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Member mobileMember = baseMapper.selectOne(wrapper);
        if (mobileMember==null){
            //为空则提示手机号不存在
            throw new CustomException(20001,"邮箱不存在");
        }
        //手机号也正确那么就验证密码是否正确
        //但是数据库中存储的密码是加密之后的，所以我们在判断之前，需要对
        //用户输入的密码进行加密，再去做判断
        if (!MD5.encrypt(password).equals(mobileMember.getPassword())){
            //不正确则给用户以提示
            throw new CustomException(20001,"用户名或密码错误");
        }

        if (mobileMember.getIsDisabled()){
            throw new CustomException(20001,"用户被禁用");
        }

        //信息全部正确 返回token字符串
        String token = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        //登录人数加1
        redisTemplate.opsForValue().increment("loginCount",1l);
        return token;
    }

    //用户注册的方法
    @Override
    public void register(RegisterVo registerVo) {
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
        //判断是否为空
        if (
                StringUtil.isNullOrEmpty(mobile)||
                StringUtil.isNullOrEmpty(nickname)||
                StringUtil.isNullOrEmpty(password)||
                StringUtil.isNullOrEmpty(code)
        ){
            throw new CustomException(20001,"信息不全");
        }
        //验证 验证码是否正确
        String verification = redisTemplate.opsForValue().get(mobile);
        if (StringUtil.isNullOrEmpty(verification)){
            throw new CustomException(20001,"验证码失效");
        }
        if (!code.equals(verification)){
            throw new CustomException(20001,"验证码错误");
        }
        //验证手机号是否重复
        QueryWrapper<Member> wrapper =new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        long count = baseMapper.selectCount(wrapper);
        if (count>0){
            throw new CustomException(20001,"用户已存在");
        }

        Member member=new Member();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("https://online-learn000.oss-cn-chengdu.aliyuncs.com/2022/06/01/e4194c7dece147a49501aac534b0c63ctouxiang.png");
        //注册成功删除验证码
        redisTemplate.delete(mobile);
        baseMapper.insert(member);
    }

    @Override
    public Member getByOpenid(String openid) {
        QueryWrapper<Member> wrapper =new QueryWrapper<>();
        wrapper.eq("openid",openid);
        Member member = baseMapper.selectOne(wrapper);
        return member;
    }

    //查询某一天的注册人数
    @Override
    public Integer countRegisterDate(String date) {
        Integer count=baseMapper.countRegister(date);
        return count;
    }

    @Override
    public void forget(RegisterVo registerVo) {
        //拿出手机号和密码
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        //判断是否为空
        if (StringUtil.isNullOrEmpty(mobile)||StringUtil.isNullOrEmpty(password)){
            throw new CustomException(20001,"信息不全");
        }
        //不为空则继续验证邮箱
        QueryWrapper<Member> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Member mobileMember = baseMapper.selectOne(wrapper);
        if (mobileMember==null){
            //为空则提示手机号不存在
            throw new CustomException(20001,"邮箱不存在");
        }
        //验证 验证码是否正确
        String verification = redisTemplate.opsForValue().get(mobile);
        if (StringUtil.isNullOrEmpty(verification)){
            throw new CustomException(20001,"验证码失效");
        }
        if (!verification.equals(registerVo.getCode())){
            throw new CustomException(20001,"验证码错误");
        }

        Member member=new Member();
        member.setMobile(mobile);
        member.setPassword(MD5.encrypt(password));
        //注册成功删除验证码
        redisTemplate.delete(mobile);
        LambdaQueryWrapper<Member> updateWrapper = new LambdaQueryWrapper<>();
        updateWrapper.eq(Member::getMobile,mobile);
        baseMapper.update(member,updateWrapper);
    }
}
